package com.tamtamy.jttamobile.utility;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.tamtamy.jttamobile.conf.JTTAProperties;
import com.tamtamy.jttamobile.constant.Type;
import com.tamtamy.jttamobile.data.Metadata;
import com.tamtamy.jttamobile.data.Token;

/**
 * @author a.pellegatta
 *
 */
public class APIUtility {

	public void setAppName(String appName) {
		this.appName = appName;
	}

	private String TAMTAMY_API_CONTEXT= "/tamtamy/api";
	private String type;
	private Token token;
	private String appName = JTTAProperties.APP_NAME;
	private String protocol = "https";
	private String hostName;
	private String baseSecureUrl;
	private String baseUrl;
	
	public final static String APP_NAME = "jTTA-mobile";
	public final static String REQUEST_XML 	= ".xml";
	public final static String REQUEST_JSON = ".json";
	
	public APIUtility( String protocol, String hostName, String type, boolean debug) {
		this.hostName = hostName;
		this.protocol = protocol;
		computeBaseUrl();
		this.type = Type.JSON.equalsIgnoreCase(type)?JTTAProperties.REQUEST_JSON:JTTAProperties.REQUEST_XML;
	}
	
	public void setHostName(String hostName ){
		this.hostName = hostName;
		computeBaseUrl();
	}
	
	public void setProtocol(String protocol){
		this.protocol = protocol;
		computeBaseUrl();
	}
	
	public String getTamTamyUrl(){
		return protocol + "://"+ hostName ;
	}
	
	private void computeBaseUrl(){
		baseUrl = protocol + "://"+ hostName + TAMTAMY_API_CONTEXT;
		baseSecureUrl = "https://"+ hostName + TAMTAMY_API_CONTEXT;
	}
	
	public String getAppNameParameter(){
		return "&appName=" + appName;
	}
	
	public NameValuePair getAppNameValuePair(){
		return new BasicNameValuePair("appName", appName);
	}

	public String getTokenParameter(){
		return "?token=" + token.getToken();
	}
	
	public String getLoginURL(String username, String password) {
		return baseSecureUrl + "/login" + type + "?userid=" + username + "&password=" + password + getAppNameParameter();
	}
	
	public String getLoginURL(String tamtamyUrl, String username, String password) {
		return "https://"+ tamtamyUrl + TAMTAMY_API_CONTEXT + "/login" + type + "?userid=" + username + "&password=" + password + getAppNameParameter();
	}
	
	public String getLogoutURL(){
		return baseUrl + "/logout" + type + getTokenParameter() + getAppNameParameter();
	}

	public String getDownloadContentURL(String contentID) {
		return baseUrl + "/contentDownload/" +contentID + getTokenParameter() + getAppNameParameter();
	}
	
	public String getCommentURL(long commentID) {
		return baseUrl + "/comment/" + commentID + type + getTokenParameter() + getAppNameParameter();
	}
 
	public String getCommentListURL(long startOffset, long limit) {
		String commentListURL = this.baseUrl + "/comment" + this.type +  getTokenParameter();
		if(startOffset>-1){
			commentListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		commentListURL += getAppNameParameter();
		return commentListURL;
	}
	
	public String getCommentsListURL(long contentID, long startOffset, long limit) {
		String commentListURL = this.baseUrl + "/comment" + this.type +  getTokenParameter();
		commentListURL += "&contentid="+contentID;
		if(startOffset>-1){
			commentListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		commentListURL += getAppNameParameter();
		return commentListURL;
	}
	
	public String getContentURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentFileURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentLinkURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getPostContentURL() {
		return baseUrl + "/content" + type ;
	}
	
	public String getContentListURL(long startOffset, long limit, String filter, String order) {
		String contentURL = baseUrl + "/content" + type + getTokenParameter();	
		contentURL += "&startOffset=" + startOffset + "&limit=" + limit;		
		if(filter!=null){
			contentURL += "&filter="+filter;
		}
		if(order!=null && !"".equals(order)){
			contentURL += "&order="+order;
		}
		contentURL += getAppNameParameter();
		return contentURL;
	}
	
	public String getContentWidgetURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}

	public String getGroupDetailsURL(String groupID) {
		return baseUrl + "/group/" + groupID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getGroupListURL(long startOffset, long limit) {
		String groupListURL = baseUrl + "/group" + type + getTokenParameter();	
		if(startOffset>-1){
			groupListURL += "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		groupListURL += getAppNameParameter();
		return groupListURL;
	}
	
	public String getMetadataURL(String url) throws Exception {
		String metadataURL = url + "/metadataGuess" + type + getTokenParameter();	
		metadataURL += "&url=" + Metadata.encodeURL(url) + getAppNameParameter(); 
		return metadataURL;
	}

	public String getNetworkDetailsURL(String networkID) {
		return baseUrl + "/network/" + networkID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getContentMessageURL(String contentID) {
		return baseUrl + "/content/" + contentID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getNetworkListURL(long startOffset, long limit) {
		String networkURL = baseUrl + "/network" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}

	public String getNetworkListURL(long startOffset, long limit, String filter) {
		String networkURL = baseUrl + "/network" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit + "&filter=" + filter;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}
	
	public String getNetworkListWithChatEnabledURL(long startOffset, long limit) {
		String networkURL = baseUrl + "/network-chat" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}

	public String getNetworkListWithChatEnabledURL(long startOffset, long limit, String filter) {
		String networkURL = baseUrl + "/network-chat" + type + getTokenParameter();		
		if(startOffset>-1){
			networkURL +=  "&startOffset=" + startOffset + "&limit=" + limit + "&filter=" + filter;	
		}	
		networkURL += getAppNameParameter();
		return networkURL;
	}
	
	public String getNoticeURL(long noticeID) {
		return baseUrl + "/notice/" + noticeID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getNoticeListURL(long startOffset, long limit, String filter, String userId, String fromDate, String toDate) {
		String noticeListURL = baseUrl + "/notice" + type + getTokenParameter();
		if(startOffset>-1){
			noticeListURL +=  "&startOffset=" + startOffset + "&limit=" + limit;
		}
		if(filter!=null){
			noticeListURL += "&filter=" + filter;
		}
		if(userId!=null){
			noticeListURL += "&userid=" + userId;
		}
		if(fromDate!=null){
			noticeListURL += "&fromDate=" + fromDate;
		}
		if(toDate!=null){
			noticeListURL += "&toDate=" + toDate;
		}
		noticeListURL += getAppNameParameter();
		return noticeListURL;
	}
	
	public String getOnlineUsersList(long startOffset, long limit) {
		String userListURL = baseUrl + "/on-line-user" + type + getTokenParameter();
		if(startOffset>-1){
			userListURL += "&startOffset=" + startOffset + "&limit=" + limit; 
		}
		userListURL += getAppNameParameter();
		return userListURL;
	}
	
	public String getTagListDetailURL(String tag) {
		return baseUrl + "/tag/" + tag + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getTagListURL(long startOffset, long limit) {
		String tagListURL = baseUrl + "/tag" + type + getTokenParameter();
		if(startOffset>-1){
			tagListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		tagListURL += getAppNameParameter();
		return tagListURL;
	}
	
	public String getUserDetailURL(String userID) {
		return baseUrl + "/user/" + userID + type + getTokenParameter() + getAppNameParameter();
	}
	
	public String getUserStatsGenerationDetailsURL(String userStatsID,	long startOffset, long limit) {
		String userStatsGenerationDetailsURL = baseUrl + "/userStatsGeneration/" + userStatsID + type + getTokenParameter();
		if(startOffset>-1){
			userStatsGenerationDetailsURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}	
		userStatsGenerationDetailsURL += getAppNameParameter();
		return userStatsGenerationDetailsURL;
	}
	
	public String getUserStatsGenerationListURL(long startOffset, long limit) {
		String userStatsGenerationListURL = baseUrl + "/user-stats-generation" + type + getTokenParameter();
		if(startOffset>-1){
			userStatsGenerationListURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		userStatsGenerationListURL += getAppNameParameter();
		return userStatsGenerationListURL;
	}

	public String getUserStatsURL(String userID, long startOffset, long limit) {
		String userStatsURL = baseUrl + "/userStats/" + userID + type + getTokenParameter();
		if(startOffset>-1){
			userStatsURL += "&startOffset=" + startOffset + "&limit=" + limit;
		}
		userStatsURL += getAppNameParameter();
		return userStatsURL;
	}

	public String getUserTagURL(String filter) {
		String userTagURL = baseUrl + "/userTag" + type + getTokenParameter();
		if(filter!=null){
		  userTagURL += "&filter=" + filter;
		}
		userTagURL += getAppNameParameter();
		return userTagURL;
	}
	
	public String getUserTagURL(String userID, String filter) {
		String userTagURL = baseUrl + "/userTag/" + userID + type + getTokenParameter();
		if(filter!=null){
		  userTagURL += "&filter=" + filter;
		}
		userTagURL += getAppNameParameter();
		return userTagURL;
	}
	
	public String getUserListURL(long startOffset, long limit, String filter) {
		String userListURL = baseUrl + "/user" + type + getTokenParameter();
		if(startOffset>-1){
			userListURL += "&startOffset=" + startOffset + "&limit="+limit;
		}
		if(filter!=null){
			userListURL += "&filter=" + filter;
		}
		userListURL += getAppNameParameter();
		return userListURL;
	}
	
	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		Log.d(this.getClass().getSimpleName(), "seToken: " + token.toString());
		this.token = token;
	}

	public String getBaseUrl() {
		return baseUrl;
	}
	
	public String getBaseSecureUrll() {
		return baseSecureUrl;
	}	
	
	
}
